#!/usr/bin/env python
from Bullet import Bullet
import random
from math import pi
from pygame import transform

class Gun:
    def init(self, image, xoff, yoff, bul_xoff, bul_yoff, bul_dammage, bul_image, sps, trail):
        self.image = image
        self.image_bak = image
        self.bak_xoff = xoff
        self.bak_yoff = yoff
        self.bak_bul_xoff = bul_xoff
        self.bak_bul_yoff = bul_yoff
        self.start_xoff = xoff[0]
        self.start_yoff = yoff[0]
        self.xoff = self.start_xoff
        self.yoff = self.start_yoff
        self.direction = 0
        
        self.bul_xoff = bul_xoff[0]
        self.bul_yoff = bul_yoff[0]
        self.bul_dammage = bul_dammage
        self.bul_image = bul_image
        self.sps = sps #shots per second
        self.bul_trail = trail #leaves trail
        self.angle = 0
    
    def update(self, xoff, yoff):
        self.xoff = xoff
        self.yoff = yoff
    
    def set_direction(self, direction):
        if self.direction != direction:
            self.direction = direction
            self.image = transform.flip(self.image, True, False)
            self.image_bak = transform.flip(self.image_bak, True, False)
            if self.direction == 0:
                self.start_xoff = self.bak_xoff[0]
                self.start_yoff = self.bak_yoff[0]
                self.bul_xoff = self.bak_bul_xoff[0]
                self.bul_yoff = self.bak_bul_yoff[0]
            else:
                self.start_xoff = self.bak_xoff[1]
                self.start_yoff = self.bak_yoff[1]
                self.bul_xoff = self.bak_bul_xoff[1]
                self.bul_yoff = self.bak_bul_yoff[1]
    
    def set_angle(self, angle):
        if self.angle != angle:
            self.angle = angle
            self.image = transform.rotate(self.image_bak, angle)
        
    def shoot(self, level, x, y, speed, direction):
        rand = float(random.randint(-1, 1)) * float(pi) / float(180)
        obj = Bullet(level, x + self.xoff + self.bul_xoff, y + self.yoff + self.bul_yoff,
                    self.bul_image, speed, direction + rand, self.bul_dammage, self.bul_trail)
        obj.solid = False
        level.update_list.append(obj)
        level.draw_list.append(obj)
        #sound bang
        
    def draw(self, camera, pos):
        camera.blit(self.image, (pos[0] + self.xoff, pos[1] + self.yoff))